<template>
  <div class="measurement-menu">
    <div class="measurement-menu-title">测量库</div>
    <div class="measurement-menu-items">
      <div
        class="measurement-menu-item"
        v-for="(subItem, subIndex) in subItems"
        :key="subIndex"
        @click="executeFunction(subItem)"
      >
        <div class="menu-item-icon">
          <IconFont class="icon" :name="subItem.icon" size="2.0833vw" />
          <!-- <img :src="subItem.icon" alt="功能图标" /> -->
        </div>
        <div class="menu-item-text">{{ subItem.text }}</div>
      </div>
    </div>
    <!-- 清除 -->
    <div class="measurement-menu-button">
      <button class="menu-button" @click="clearRecently">撤销</button>
      <button class="menu-button" @click="clearAll">清除全部测量</button>
    </div>

    <div class="surface-start-button" v-if="startESSurfaceAreaMeasurement">
      <el-button type="primary" class="action-btn" @click="start">开始表面积分析</el-button>
    </div>
  </div>
</template>

<script setup>
import { getobjm } from '@/utils/getobjm.js'
import { ref, onUnmounted } from 'vue'
import {
  ESLocationMeasurement, //点位测量
  ESDistanceMeasurement, //距离测量
  ESHeightMeasurement, //高度测量
  ESDirectionMeasurement, //角方位测量
  ESAreaMeasurement, //面积测量
  ESSurfaceAreaMeasurement, //表面积测量
} from 'earthsdk3'
import { useMeasurementStore } from '@/stores/measurementStore.js'
import { useTreeStore } from '@/stores/treeStore.js'
import { useStartAnalysisStore } from '@/stores/startAnalysis.js'
// 使用 Pinia store
const measurementStore = useMeasurementStore()
const treeStore = useTreeStore()
const startAnalysisStore = useStartAnalysisStore()
const objm = getobjm()
const subItems = [
  {
    text: '点位测量',
    icon: 'dw',
    // icon: new URL('@/assets/images/sidebar/easurementLibrary/clTest.png', import.meta.url).href,
    action: 'createESLocationMeasurement',
  },
  {
    text: '距离测量',
    icon: 'jl',
    action: 'createESDistanceMeasurement',
  },
  {
    text: '高度测量',
    icon: 'gd',
    action: 'createESHeightMeasurement',
  },
  {
    text: '角方位测量',
    icon: 'jfw',
    action: 'createESDirectionMeasurement',
  },
  {
    text: '面积测量',
    icon: 'mj',
    action: 'createESAreaMeasurement',
  },
  {
    text: '表面积测量',
    icon: 'bmj',
    action: 'createESSurfaceAreaMeasurement',
  },
]
// 定义emit，用于通知父组件关闭子菜单
const emit = defineEmits(['close-submenu'])
// 功能执行方法
const executeFunction = (subItem) => {
  switch (subItem.action) {
    case 'createESLocationMeasurement':
      createMeasuringObject(ESLocationMeasurement)
      emit('close-submenu') // 正常关闭子菜单
      break
    case 'createESDistanceMeasurement':
      createMeasuringObject(ESDistanceMeasurement)
      emit('close-submenu') // 正常关闭子菜单
      break
    case 'createESHeightMeasurement':
      createMeasuringObject(ESHeightMeasurement)
      emit('close-submenu') // 正常关闭子菜单
      break
    case 'createESDirectionMeasurement':
      createMeasuringObject(ESDirectionMeasurement)
      emit('close-submenu') // 正常关闭子菜单
      break
    case 'createESAreaMeasurement':
      createMeasuringObject(ESAreaMeasurement)
      emit('close-submenu') // 正常关闭子菜单
      break
    case 'createESSurfaceAreaMeasurement':
      createESSurfaceAreaMeasurement() //// 不关闭子菜单，等待表面积测量分析
      break
    default:
      console.log(`未知功能: ${subItem.action}`)
      // 执行功能后通知父组件关闭子菜单
      emit('close-submenu')
  }
}
//创建测量
const createMeasuringObject = (sceneObject) => {
  const measuringObject = objm.createSceneObject(sceneObject)
  if (!measuringObject) return
  measuringObject.editing = true
  measuringObject.editingChanged.disposableWeakOn(() => {
    if (measuringObject && measuringObject.editing === false) {
      const json = measuringObject.json
      //点位测量
      if (json.type === 'ESLocationMeasurement') {
        const position = measuringObject.position
        const a = position[0] === 0 && position[1] === 0
        setTimeout(() => {
          if (!a) {
            measurementStore.createMeasurementData.push(measuringObject)
          }
        }, 300)
      } else if (
        //距离 高度 角方位
        json.type === 'ESDistanceMeasurement' ||
        json.type === 'ESHeightMeasurement' ||
        json.type === 'ESDirectionMeasurement'
      ) {
        const pos = measuringObject.points?.length
        setTimeout(() => {
          if (pos && pos >= 2) {
            measurementStore.createMeasurementData.push(measuringObject)
          }
        }, 300)
      } else if (
        //面积
        json.type === 'ESAreaMeasurement'
      ) {
        const pos = measuringObject.points?.length
        setTimeout(() => {
          if (pos && pos >= 3) {
            measurementStore.createMeasurementData.push(measuringObject)
          }
        }, 300)
      }
    }
  })
}
//创建测量对象
//#region
// // 点位测量
// const createESLocationMeasurement = () => {
//   const sceneObjectESLocationMeasurement = objm.createSceneObject(ESLocationMeasurement)
//   if (!sceneObjectESLocationMeasurement) return
//   measurementStore.createMeasurementData.push(sceneObjectESLocationMeasurement)
//   sceneObjectESLocationMeasurement.editing = true
//   console.log(measurementStore.createMeasurementData)
// }

// // 距离测量
// const createESDistanceMeasurement = () => {
//   const sceneObjectESDistanceMeasurement = objm.createSceneObject(ESDistanceMeasurement)
//   if (!sceneObjectESDistanceMeasurement) return
//   measurementStore.createMeasurementData.push(sceneObjectESDistanceMeasurement)
//   sceneObjectESDistanceMeasurement.editing = true
// }
// //高度测量
// const createESHeightMeasurement = () => {
//   const sceneObjectESHeightMeasurement = objm.createSceneObject(ESHeightMeasurement)
//   treeStore.addSceneObjectToTreeFromClass(ESHeightMeasurement)
//   measurementStore.createMeasurementData.push(sceneObjectESHeightMeasurement)
//   sceneObjectESHeightMeasurement.editing = true
// }
// //角方位测量
// const createESDirectionMeasurement = () => {
//   const sceneObjectESDirectionMeasurement = objm.createSceneObject(ESDirectionMeasurement)
//   measurementStore.createMeasurementData.push(sceneObjectESDirectionMeasurement)
//   sceneObjectESDirectionMeasurement.editing = true
// }
// //面积测量
// const createESAreaMeasurement = () => {
//   const sceneObjectESAreaMeasurement = objm.createSceneObject(ESAreaMeasurement)
//   measurementStore.createMeasurementData.push(sceneObjectESAreaMeasurement)
//   sceneObjectESAreaMeasurement.editing = true
// }
//#endregion

//表面积测量
const sceneObjectESSurfaceAreaMeasurement = ref(null)
//表面积分析
const startESSurfaceAreaMeasurement = ref(false)
const createESSurfaceAreaMeasurement = () => {
  //创建了对象,但是没有分析   分析过会清对象
  if (sceneObjectESSurfaceAreaMeasurement.value) {
    cleanupSurfaceAreaAnalysis()
  }
  sceneObjectESSurfaceAreaMeasurement.value = objm.createSceneObject(ESSurfaceAreaMeasurement)
  if (!sceneObjectESSurfaceAreaMeasurement.value) return
  sceneObjectESSurfaceAreaMeasurement.value.editing = true
  startAnalysisStore.startAnalysisState = true
  // 表面积测量分析
  sceneObjectESSurfaceAreaMeasurement.value.editingChanged.disposableWeakOn(() => {
    if (
      sceneObjectESSurfaceAreaMeasurement.value &&
      sceneObjectESSurfaceAreaMeasurement.value.editing === false
    ) {
      const pos = sceneObjectESSurfaceAreaMeasurement.value.points?.length
      if (pos && pos >= 3) {
        startESSurfaceAreaMeasurement.value = true
      }
    }
  })
}

//开始分析
const start = async (event) => {
  // 阻止事件冒泡
  if (event) {
    event.stopPropagation()
  }
  await sceneObjectESSurfaceAreaMeasurement.value.start()
  //完成了分析才能加入
  measurementStore.createMeasurementData.push(sceneObjectESSurfaceAreaMeasurement.value)
  startESSurfaceAreaMeasurement.value = false
  startAnalysisStore.startAnalysisState = false
  sceneObjectESSurfaceAreaMeasurement.value = null
  emit('close-submenu')
}

// 清理未完成的表面积分析
const cleanupSurfaceAreaAnalysis = () => {
  // //创建了对象,但是没有分析   分析过会清对象
  if (sceneObjectESSurfaceAreaMeasurement.value) {
    // 销毁场景对象
    objm.destroySceneObject(sceneObjectESSurfaceAreaMeasurement.value)
    // 重置状态
    sceneObjectESSurfaceAreaMeasurement.value = null
    startESSurfaceAreaMeasurement.value = false
    startAnalysisStore.startAnalysisState = false //
  }
}

// 组件销毁前的清理工作
onUnmounted(() => {
  cleanupSurfaceAreaAnalysis()
  startAnalysisStore.startAnalysisState = false
})

//撤回最近一次测量
const clearRecently = () => {
  if (startESSurfaceAreaMeasurement.value) {
    // 销毁场景对象
    objm.destroySceneObject(sceneObjectESSurfaceAreaMeasurement.value)
    startAnalysisStore.startAnalysisState = false
    startESSurfaceAreaMeasurement.value = false
    return
  }
  if (measurementStore.hasCreateMeasurementData) {
    // 获取数组最后一项（最近创建的测量）
    const recently = measurementStore.createMeasurementData.pop() //1.删除数组中的最后一个元素 2.返回被删除的元素
    // 销毁该测量对象
    objm.destroySceneObject(recently)
  }
}
//清除全部测量
const clearAll = () => {
  if (!measurementStore.hasCreateMeasurementData) return
  measurementStore.createMeasurementData.forEach((item) => {
    if (item) objm.destroySceneObject(item)
  })
  measurementStore.createMeasurementData = []
}
</script>

<style lang="scss" scoped>
.measurement-menu {
  width: 15.625vw /* 300px -> 15.625vw */;
  background-color: rgba(0, 8, 26, 0.8);
  border-radius: 0.4167vw /* 8px -> .4167vw */;
  box-shadow: 0 0 0.5208vw /* 10px -> .5208vw */ rgba(0, 195, 255, 0.5);
  border: 0.0521vw /* 1px -> .0521vw */ solid rgba(0, 195, 255, 0.6);
  padding: 0.5208vw /* 10px -> .5208vw */;
  z-index: 2;
  .measurement-menu-title {
    font-size: 0.8333vw /* 16px -> .8333vw */;
    color: #00c3ff;
    font-weight: bold;
    padding-bottom: 0.5208vw /* 10px -> .5208vw */;
    border-bottom: 0.0521vw /* 1px -> .0521vw */ solid rgba(0, 195, 255, 0.3);
  }

  .measurement-menu-items {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.5208vw /* 10px -> .5208vw */;
    padding: 0.5208vw /* 10px -> .5208vw */ 0;
    flex-wrap: wrap;
    max-height: 10.9375vw /* 210px -> 10.9375vw */;
    overflow: auto;
    // 自定义滚动条样式
    &::-webkit-scrollbar {
      width: 0.4167vw /* 8px -> .4167vw */; // 滚动条宽度
    }

    &::-webkit-scrollbar-track {
      background: rgba(0, 0, 0, 0.3); // 滚动条轨道背景
      border-radius: 0.2083vw /* 4px -> .2083vw */; // 轨道圆角
    }

    &::-webkit-scrollbar-thumb {
      background: rgba(0, 195, 255, 0.5); // 滚动条滑块颜色
      border-radius: 0.2083vw /* 4px -> .2083vw */; // 滑块圆角
    }
    .measurement-menu-item {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      gap: 0.5208vw /* 10px -> .5208vw */;
      width: 4.1667vw /* 80px -> 4.1667vw */;
      height: 4.1667vw /* 80px -> 4.1667vw */;
      cursor: pointer;
      border-radius: 0.2083vw /* 4px -> .2083vw */;
      &:hover {
        background-color: rgba(0, 195, 255, 0.2);
        .menu-item-icon {
          .icon {
            scale: 0.8;
          }
        }
      }
      .menu-item-icon {
        .icon {
          width: 2.0833vw /* 40px -> 2.0833vw */;
          height: 2.0833vw /* 40px -> 2.0833vw */;
          object-fit: contain;
          transition: all 0.5s;
        }
      }
      .menu-item-text {
        height: 0.8333vw /* 16px -> .8333vw */;
        line-height: 0.8333vw /* 16px -> .8333vw */;
        font-size: 0.625vw /* 12px -> .625vw */;
        color: #ffffff;
        text-align: center;
      }
    }
  }
  .measurement-menu-button {
    padding-top: 0.5208vw /* 10px -> .5208vw */;
    border-top: 0.0521vw /* 1px -> .0521vw */ solid rgba(0, 195, 255, 0.3);
    display: flex;
    justify-content: space-between;
    align-items: center;
    .menu-button {
      background-color: rgba(0, 195, 255, 0.2);
      border: 0.0521vw /* 1px -> .0521vw */ solid rgba(0, 195, 255, 0.6);
      color: #00c3ff;
      padding: 0.2604vw /* 5px -> .2604vw */ 0.4167vw /* 8px -> .4167vw */;
      border-radius: 0.2083vw /* 4px -> .2083vw */;
      font-size: 12px;
      cursor: pointer;
      &:hover {
        background-color: rgba(0, 195, 255, 0.4);
      }
    }
  }
  // /* 修改表面积分析按钮样式 */
  // .surface-area-start-button {
  //   margin-top: 0.5208vw;
  //   padding: 0.5208vw 1.0417vw;
  //   background-color: rgba(0, 195, 255, 0.4);
  //   border: 0.0521vw solid rgba(0, 195, 255, 0.8);
  //   border-radius: 0.2083vw;
  //   color: white;
  //   font-weight: bold;
  //   text-align: center;
  //   cursor: pointer;
  //   transition: all 0.3s;

  //   &:hover {
  //     background-color: rgba(0, 195, 255, 0.6);
  //     box-shadow: 0 0 0.5208vw rgba(0, 195, 255, 0.8);
  //   }
  // }

  .surface-start-button {
    margin-top: 0.5208vw /* 10px -> .5208vw */;
    :deep(.el-button) {
      width: 100%;
      background-color: rgba(0, 195, 255, 0.2);
      border: 0.0521vw /* 1px -> .0521vw */ solid rgba(0, 195, 255, 0.6);
      color: #00c3ff;
      border-radius: 0.2083vw /* 4px -> .2083vw */;
      font-size: 0.625vw /* 12px -> .625vw */;
      height: 1.6667vw /* 32px -> 1.6667vw */;
      line-height: 1.6667vw /* 32px -> 1.6667vw */;
      margin: 0;
      &:hover {
        background-color: rgba(0, 195, 255, 0.4);
      }
    }
  }
}
</style>
